<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./node_modules/axios/dist/axios.min.js"></script>
  <style>
    body {
      margin: 0;
      padding: 30px;
    }
  </style>
</head>

<body>
  <h1>修改用户</h1>
  用户名：<input type="text" id="username"><br>
  密码：<input type="text" id="password"><br>
  <button>提交</button>


  <script>
    //获取从首页index.html页面携带过来的id值(手动解析)
    /* console.log(location.search.split('='))
    const id = location.search.split('=')[1]
    console.log(id) */

    //自动解析
    const id = new URLSearchParams(location.search).get('id')
    console.log(id)

    //发送axios的get请求，获取要修改用户的数据，进行回显操作
    axios.get(`http://localhost:3000/users/${id}`).then(res => {

      //获取要回显的用户数据
      document.querySelector('#username').value = res.data.username
      document.querySelector('#password').value = res.data.password

    })

    //给确定按钮添加点击事件
    document.querySelector('button').addEventListener('click', function () {

      //获取修改的元素值
      const username = document.querySelector('#username').value
      const password = document.querySelector('#password').value

      //修改后台对应id值得元素
      axios.put(`http://localhost:3000/users/${id}`, { username: username, password: password }).then(res => {

        //跳转到index界面
        location.href = './index.html'
      })
    })
  </script>
</body>

</html>